package cn.raven._02_asynchronous;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class Producer {

    public static void main(String[] args) throws Exception {
        //创建生产者
        DefaultMQProducer producer = new DefaultMQProducer("my_asynchronous_producer_name");
        //设置nameserver
        producer.setNamesrvAddr("localhost:9876");
        //启动
        producer.start();
        //创建信息对象
        Message msg = new Message("xxx", "yyy", "jjking".getBytes(StandardCharsets.UTF_8));
        //发送异步消息
        producer.send(msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("发送失败");
            }
        });
    }
}
